﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Thor.Common;
using Thor.Domain;

namespace Thor.SharePoint2007.WebServices
{
    internal class SPCalendar
    {
        public static XmlNode GetQueryNode(CalendarRequest calendarRequest)
        {
            XmlDocument queryDoc = new XmlDocument();
            XmlNode queryNode = queryDoc.CreateNode(XmlNodeType.Element, "Query", "");

            queryNode.InnerXml = @"<Where><And><Geq><FieldRef Name='EventDate' /><Value Type='DateTime'><Today /></Value></Geq><Leq><FieldRef Name='EndDate' /><Value Type='DateTime'><Today /></Value></Leq></And></Where>";

            return queryNode;
        }

        public static Calendar GetCalendar(XmlNode sharePointResponse)
        {
            Calendar calendar = new Calendar();
            calendar.Appointments = new List<Appointment>();

            var nodeList = GetList(sharePointResponse.ChildNodes);

            if (nodeList != null)
            {
                var dataNodes = GetDataNodes(nodeList);

                if (dataNodes != null)
                {
                    foreach (var dataNode in dataNodes)
                    {
                        var childNodeList = GetList(dataNode.ChildNodes);

                        if (childNodeList != null)
                        {
                            var rowNodes = GetRowNodes(childNodeList);

                            if (rowNodes != null)
                            {
                                foreach (var rowNode in rowNodes)
                                {
                                    Appointment appointment = new Appointment();
                                    
                                    if (rowNode.Attributes["ows_EventDate"].Value.FromSharePointString().HasValue)
                                        appointment.StartTime = rowNode.Attributes["ows_EventDate"].Value.FromSharePointString().Value;

                                    if (rowNode.Attributes["ows_EndDate"].Value.FromSharePointString().HasValue)
                                        appointment.StartTime = rowNode.Attributes["ows_EndDate"].Value.FromSharePointString().Value;

                                    appointment.Subject = rowNode.Attributes["ows_Title"].Value;

                                    appointment.Id = Guid.NewGuid();

                                    calendar.Appointments.Add(appointment);
                                }
                            }
                        }
                    }
                }
            }

            return calendar;
        }

        private static List<XmlNode> GetList(XmlNodeList source)
        {
            if (source != null)
            {
                List<XmlNode> result = new List<XmlNode>();

                for (int i = 0; i < source.Count; i++)
                    result.Add(source[i]);

                return result;
            }

            return null;
        }

        private static List<XmlNode> GetDataNodes(List<XmlNode> source)
        {
            if (source != null)
                return GetNodesByName(source, "rs:data");
            else
                return null;
        }

        private static List<XmlNode> GetRowNodes(List<XmlNode> source)
        {
            if (source != null)
                return GetNodesByName(source, "z:row");
            else
                return null;
        }

        private static List<XmlNode> GetNodesByName(List<XmlNode> source, string name)
        {
            if (source != null)
            {
                return (from item in source
                        where item.Name == name
                        select item).ToList();
            }
            else
                return null;
        }
    }
}